//
//  ZCEmoticonToolBar.swift
//  weibo
//
//  Created by Ning Xie on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum emoticonToolBarType:Int{
    case leftBtn = 1000
    case middleBtn = 1001
    case rightBtn = 1002
}


class ZCEmoticonToolBar: UIStackView {

    
    var didSelectedButtonClosure:((type:emoticonToolBarType)->())?
     var selectedButton:UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        axis = .Horizontal
        distribution = .FillEqually
        
        
        addChildButton("默认", imageName: "left",type: .leftBtn)
        addChildButton("Emoji", imageName: "mid",type: .middleBtn)
        addChildButton("浪小花", imageName: "right",type: .rightBtn)
    }
    
    func addChildButton(title:String,imageName:String,type:emoticonToolBarType){
        
        let button = UIButton(type: UIButtonType.Custom)
        button.tag = type.rawValue
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imageName)_selected"), forState: .Selected)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: #selector(ZCEmoticonToolBar.buttonAction(_:)), forControlEvents: .TouchUpInside)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        if type == .leftBtn{
            selectedButton?.selected = false
            button.selected = true
//            button.userInteractionEnabled = false
            selectedButton = button
        }
        addArrangedSubview(button)
    }
    
    @objc private func buttonAction(button:UIButton){
        
        if selectedButton == button {
            return
        }
        selectedButton?.selected = false
//        selectedButton?.userInteractionEnabled = true
        
        button.selected = true
//        button.userInteractionEnabled = false
        
        selectedButton = button
        
        let type = emoticonToolBarType(rawValue: button.tag)!
        didSelectedButtonClosure?(type:type)
        

    }
    
    func selectedEmoticonButton(section:Int){
        
        let button = viewWithTag(section+1000) as! UIButton
        
        if selectedButton == button {
            
            return
        }
        
        selectedButton?.selected = false
//        selectedButton?.userInteractionEnabled = true
        
        button.selected = true
//        button.userInteractionEnabled = false
        
        selectedButton = button

        
    }

}
